package com.example.demo2.demo00;

import com.example.demo2.rabbitmq.RabbitMQConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @Description: 消费者
 * @Author: mingtian
 * @CreateDate: 2021/1/16 09:10
 * @Version: 1.0
 */
public class Producer {

    /**
     * 队列名称
     */
    private static final String QUEUE_NAME = "test_queue";

    /**
     * 生产者消息确认机制
     * //     *
     * //     * @param args
     * //
     */
    public static void main(String[] args) {
        try {
            //1.创建一个新连接
            Connection connection = RabbitMQConnection.getConnection();
            //2.设置channel
            Channel channel = connection.createChannel();
            //3.发送消息
            String msg = "发送消息测试";
            channel.confirmSelect();
            // 新增发送消息成功监听
            channel.addConfirmListener(new RabbitConfirmListener());
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            boolean result = channel.waitForConfirms();
            if (result) {
                System.out.println("消息投递成功");
            } else {
                System.out.println("消息投递失败");
            }
            channel.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    /**
//     * rabbitmq事务消息
//     */
//    public static void main(String[] args) throws IOException, TimeoutException {
//        //1.创建一个新连接
//        Connection connection = RabbitMQConnection.getConnection();
//        //2.设置channel
//        Channel channel = connection.createChannel();
//        try {
//            //3.发送消息
//            String msg = "hello,this is msg";
////            channel.txSelect();
//            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
//            int i = 1 / 0;
////            channel.txCommit();
//            System.out.println("消息投递成功");
//            channel.close();
//            connection.close();
//        } catch (Exception e) {
////            if (channel != null)
////                channel.txRollback();// 回滚
//            e.printStackTrace();
//        }
//    }
}
